//
//  XDCollectionItemCell.m
//  XDEducation
//
//  Created by apple on 2017/4/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDCollectionItemCell.h"
#import "XDHomeItemView.h"

@interface XDCollectionItemCell ()
{
    NSArray <NSDictionary *> *_dataArr;
}
@end



@implementation XDCollectionItemCell


+(instancetype)cellItemArray:(NSArray<NSDictionary *> *)itemArray tableView:(UITableView *)tableView{

    static NSString *cellId = @"cell";
    
    
    XDCollectionItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        
        cell = [[XDCollectionItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId itemArray:itemArray];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier itemArray:(NSArray<NSDictionary *> *)itemArray{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _dataArr = itemArray;
        
        CGFloat itemW = 75 * ScaleWidth;
        CGFloat itemH = 80 * ScaleHeight;
        CGFloat padding = (ScreenW - 4 * itemW)/5;
        
        CGFloat itemX = padding;
        CGFloat itemY = 0;

        int tempI = 0;
        int tempJ = 0;
        for (int i = 0; i < itemArray.count; i ++) {
            
            NSString *itemName = itemArray[i][@"itemName"];
            NSString *itemIcon = itemArray[i][@"itemIcon"];
            
            
            BOOL isMultipleOfFour = !(i % 4);
            
            if (isMultipleOfFour && i != 0) {
                tempJ ++;
                tempI = 0;
                itemY = 0 + (itemH + 0) * tempJ;
            }

            
            XDHomeItemView *item = [[XDHomeItemView alloc]initWithFrame:CGRectMake(itemX + tempI * (padding + itemW), itemY , itemW, itemH) andTitle:itemName imageName:itemIcon];
            
            [self.contentView addSubview:item];
            
            tempI ++;
            
            item.tag = i;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(itemClicked:)];
            [item addGestureRecognizer:tap];
        }
        
      
        
    }
    return self;
}


-(void)itemClicked:(UITapGestureRecognizer *)tap{
 
    NSString *vcTitle = _dataArr[tap.view.tag][@"itemName"];
    
    if (self.itemPushBlock) {
        self.itemPushBlock(vcTitle);
    }
}

@end
